import 'package:event_bus/event_bus.dart';
import 'package:razer_one/app/main/constants/app_constants.dart';
import 'package:razer_one/app/main/model/user_model.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AppManager {
  //单例
  static final AppManager instance = AppManager._privateConstructor();
  AppManager._privateConstructor();
  UserModel userModel = UserModel();
  //全局通知
  EventBus? eventBus;

  factory AppManager() {
    return instance;
  }

  //取出账号
  get account async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString(kUserAccount);
  }

  //取出密码
  get passWord async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString(kUserPassWord);
  }

  //保存账号密码
  saveUser(String account, String password) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    //存储账号密码
    prefs.setString(kUserAccount, account);
    prefs.setString(kUserPassWord, password);
  }

  //退出登录清空所有数据
  clean() async {
    userModel = UserModel();

    SharedPreferences prefs = await SharedPreferences.getInstance();
    //清除账号密码
    prefs.remove(kUserAccount);
    prefs.remove(kUserPassWord);
  }
}
